package cn.iocoder.yudao.module.shop.controller.admin.memberhealth;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.shop.controller.admin.memberhealth.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.memberhealth.ShopMemberHealthDO;
import cn.iocoder.yudao.module.shop.convert.memberhealth.ShopMemberHealthConvert;
import cn.iocoder.yudao.module.shop.service.memberhealth.ShopMemberHealthService;

@Tag(name = "管理后台 - 收支记录")
@RestController
@RequestMapping("/shop/member-health")
@Validated
public class ShopMemberHealthController {

    @Resource
    private ShopMemberHealthService memberHealthService;

    @PostMapping("/create")
    @Operation(summary = "创建健康记录")
    @PreAuthorize("@ss.hasPermission('shop:member-health:create')")
    public CommonResult<Long> createMemberHealth(@Valid @RequestBody ShopMemberHealthCreateReqVO createReqVO) {
        return success(memberHealthService.createMemberHealth(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新健康记录")
    @PreAuthorize("@ss.hasPermission('shop:member-health:update')")
    public CommonResult<Boolean> updateMemberHealth(@Valid @RequestBody ShopMemberHealthUpdateReqVO updateReqVO) {
        memberHealthService.updateMemberHealth(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除健康记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('shop:member-health:delete')")
    public CommonResult<Boolean> deleteMemberHealth(@RequestParam("id") Long id) {
        memberHealthService.deleteMemberHealth(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得健康记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('shop:member-health:query')")
    public CommonResult<ShopMemberHealthRespVO> getMemberHealth(@RequestParam("id") Long id) {
        ShopMemberHealthDO memberHealth = memberHealthService.getMemberHealth(id);
        return success(ShopMemberHealthConvert.INSTANCE.convert(memberHealth));
    }

    @GetMapping("/id")
    @Operation(summary = "获得健康记录")
    @Parameter(name = "memberId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('shop:member-health:query')")
    public CommonResult<ShopMemberHealthRespVO> getMemberHealthByUserId(@RequestParam("memberId") Long memberId) {
        ShopMemberHealthDO memberHealth = memberHealthService.getMemberHealthByUserId(memberId);
        return success(ShopMemberHealthConvert.INSTANCE.convert(memberHealth));
    }

    @GetMapping("/list")
    @Operation(summary = "获得健康记录")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('shop:member-health:query')")
    public CommonResult<List<ShopMemberHealthRespVO>> getMemberHealthList(@RequestParam("ids") Collection<Long> ids) {
        List<ShopMemberHealthDO> list = memberHealthService.getMemberHealthList(ids);
        return success(ShopMemberHealthConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得健康记录")
    @PreAuthorize("@ss.hasPermission('shop:member-health:query')")
    public CommonResult<PageResult<ShopMemberHealthRespVO>> getMemberHealthPage(@Valid ShopMemberHealthPageReqVO pageVO) {
        PageResult<ShopMemberHealthDO> pageResult = memberHealthService.getMemberHealthPage(pageVO);
        return success(ShopMemberHealthConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出健康记录")
    @PreAuthorize("@ss.hasPermission('shop:member-health:export')")
    @OperateLog(type = EXPORT)
    public void exportMemberHealthExcel(@Valid ShopMemberHealthExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ShopMemberHealthDO> list = memberHealthService.getMemberHealthList(exportReqVO);
        // 导出 Excel
        List<ShopMemberHealthExcelVO> datas = ShopMemberHealthConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "收支记录.xls", "数据", ShopMemberHealthExcelVO.class, datas);
    }

}
